package com.luckku.boot.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.luckku.boot.common.api.ApiResult;
import com.luckku.boot.model.dto.LoginDTO;
import com.luckku.boot.model.entity.Coupon;
import com.luckku.boot.model.entity.CouponUser;
import com.luckku.boot.model.vo.CouponVo;
import com.luckku.boot.service.ICouponService;
import com.luckku.boot.service.ICouponUserService;
import com.luckku.boot.utils.SnowFlakeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author luckku
 * @date 2021/12/15
 */

@Controller
@RequestMapping("/coupon")
public class CouponController {

    @Autowired
    ICouponService couponService;

    @Autowired
    ICouponUserService couponUserService;

    @Autowired
    SnowFlakeUtil snowFlakeUtil;

    /**
     * /coupon/userCoupon
     * 获取用户优惠券
     */
    @ResponseBody
    @GetMapping("/userCoupon")
    public ApiResult<List<CouponVo>> getUserCoupon(@RequestParam("userId") String userId) {
        List<CouponVo> list = couponService.getUserCoupon(userId);
        return ApiResult.success(list);
    }

    /**
     * /coupon/usableCoupon
     * 获取用户优惠券
     */
    @ResponseBody
    @GetMapping("/usableCoupon")
    public ApiResult<List<CouponVo>> getUsableCoupon(@RequestParam("userId") String userId, @RequestParam("totalPrice") Long totalPrice) {
        List<CouponVo> list = couponService.getUsableCoupon(userId, totalPrice);
        return ApiResult.success(list);
    }

    /**
     * /coupon/addCoupon
     * 新增优惠券
     */
    @ResponseBody
    @PostMapping("/addCoupon")
    public ApiResult<Coupon> addCoupon(@RequestBody Coupon coupon) {
        coupon.setCouponId(String.valueOf(snowFlakeUtil.snowflakeId()));
        couponService.save(coupon);
        return ApiResult.success(coupon);
    }

    /**
     * /coupon/couponListPage
     * 以list形式获取所有优惠券（带分页参数）
     */
    @ResponseBody
    @GetMapping("/couponListPage")
    public ApiResult<Page<Coupon>> list(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                        @RequestParam(value = "size", defaultValue = "10") Integer pageSize) {
        Page<Coupon> list = couponService.getList(new Page<>(pageNo, pageSize));
        return ApiResult.success(list);
    }

    /**
     * /coupon/userGetCoupon
     * 用户获取优惠券
     */
    @ResponseBody
    @PostMapping("/userGetCoupon")
    public ApiResult<String> userGetCoupon(@RequestParam("userId") String userId, @RequestParam("couponId") String couponId) {
        Coupon coupon = couponService.getById(couponId);
        Long remainNum = coupon.getRemainNum() - 1;
        couponService.update(
                new UpdateWrapper<Coupon>().eq("coupon_id", couponId)
                .set("remain_num", remainNum)
        );
        CouponUser one = couponUserService.getOne(
                new QueryWrapper<CouponUser>().eq("user_id", userId)
                        .eq("coupon_id", couponId)
        );
        if (one==null){
            CouponUser couponUser = CouponUser.builder().userId(userId).couponId(couponId).count(1L).build();
            couponUserService.save(couponUser);
        } else {
            couponUserService.update(
                    new UpdateWrapper<CouponUser>().eq("user_id", userId)
                            .eq("coupon_id", couponId).set("count",one.getCount()+1)
            );
        }

        return ApiResult.success("领取成功");
    }

    /**
     * /coupon/couponList
     * 以list形式获取所有优惠券（带分页参数）
     */
    @ResponseBody
    @GetMapping("/couponList")
    public ApiResult<List<Coupon>> getCouponList() {
        List<Coupon> list = couponService.list();
        return ApiResult.success(list);
    }

    /**
     * /coupon/availableCouponList
     * 以list形式获取所有可以领取的优惠券
     */
    @ResponseBody
    @GetMapping("/availableCouponList")
    public ApiResult<List<Coupon>> getavailableCouponList() {
        List<Coupon> list = couponService.list(
                new QueryWrapper<Coupon>().eq("coupon_state", 1)
        );
        return ApiResult.success(list);
    }

    /**
     * /coupon/changeCouponState
     * 设置优惠券领取状态
     */
    @ResponseBody
    @PostMapping("/changeCouponState")
    public ApiResult<String> changeCouponState(@RequestParam("couponId") String couponId, @RequestParam("couponState") Long couponState) {
        Coupon coupon = couponService.getById(couponId);
        coupon.setCouponState(couponState);
        couponService.updateById(coupon);
        return ApiResult.success();
    }

}
